<?php
/*!
 * @version		$Id: controller.php 8841 2007-12-06 20:27:59Z $
 * @package		diduo
 * @link        http://www.diduo.net
 * @author      moolao <moolao@diduo.net>
 * @abstract    controller.php:  Base class for a diduo app Controller
 * @discussion  system core class (app top-layer) controller provides basic functionality.
 * @copyright	Copyright (C) 2007 - 2008 diduo.net development team. All rights reserved.
 */ 
class Controller 
{
    /** @access private */
    public $app, $selectRs, $tpl;
	/**#@-*/

	/**
	 * Class constructor, overridden in descendant classes.
	 */
	public function __construct( $app ) {
		$this->app =& $app;
		$this->selectRs =& $this->app->databaseLink(); 
		$this->tpl =& $this->app->tpl();
	}

	/** 
	 * Execute Application , and check
 	 */
    public function execute() {
	  // if request param exists get or set a default value to called 
	  // class and method 
	  $_play = $this->escapeRequest( $_REQUEST[ACTIONSTR] ) != ''
			 ? DPATH_CONTROLLER. DS. $this->escapeRequest( $_REQUEST[ACTIONSTR] ) . '.controller.php'
			 : DPATH_CONTROLLER. DS. $this->app->defaultAction . '.controller.php';
	  $_key  = $this->escapeRequest( $_REQUEST[EXEKEYSTR] ) != ''
			 ? $this->escapeRequest( $_REQUEST[EXEKEYSTR] )
			 : 'defaultKey'; 

      if ( $this->_isValidPlay( $_play ) == false ){ $this->app->catchErr( 'Error Controller Name!' ); }
	  if ( $this->_isValidKey(  $_key )  == false ){ $this->app->catchErr( 'Error Method Name!' ); }

	  $this->$_key(); // execute func default exec defaultKey
      $this->_getTPLDST(); 
	}
	
	/**
     * Detects template < @see _getTPLDST() > 
	 * Detects template's path is valid and return the tpl name
     */
	private function _setTplDST(){
	  $_execFile  = $this->escapeRequest( $_REQUEST[ACTIONSTR] )  !=  ''
	              ? $this->escapeRequest( $_REQUEST[ACTIONSTR] )
				  : $this->app->defaultAction; // public properties
      $_execFile  = $this->tpl->template_dir. DS. $_execFile. '.tpl'; 
      if ( $this->_isValidTPL( $_execFile ) == false ){ $this->app->catchErr( 'Error Template Name!' ); }
	  return $_execFile;
    }

	/** 
	 * Request param filters 
	 *
	 * @param string $param
	 * @return	string
 	 */
	protected function escapeRequest( $param ){
		return mysql_escape_string( htmlspecialchars( trim( $param ) ) );
	}

	/** 
	 * Request param filters 
	 *
	 * @param string $param
	 * @return	boolean
 	 */
    private function _isValidPlay( $key ){
	  return file_exists( $key );
    }

	/** 
	 * Request param filters 
	 *
	 * @param string $param
	 * @return	boolean
 	 */
    protected function _isValidKey( $key ){
	  return method_exists( $this , $key );
    }

	/**
	 * Checked file and returns an boolean
	 * 
	 * @param string $file
	 * @return	boolean
 	 */ 
    private function _isValidTPL( $file ){
	  return file_exists( $file );
    } 

    /**
     * Display template < @see _setTplDST() > 
     */
    private function _getTPLDST(){ 
      return $this->tpl->display($this->_setTplDST());
    }
}
 ?>